package net.huashitong.supervision.task.service;

import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.*;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.dao.TaskBackNewDao;
import net.huashitong.supervision.task.dataBean.AttachmentsDTO;
import net.huashitong.supervision.task.entity.*;
import net.huashitong.supervision.task.filter.SpecialTaskFilter;
import net.huashitong.supervision.task.filter.TaskBackNewFilter;
import net.huashitong.supervision.task.filter.TaskMainFilter;
import net.huashitong.supervision.task.filter.TaskUnitFilter;
import net.huashitong.supervision.task.util.HolidayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/20
 * Time: 9:20
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class TaskBackNewService extends StringPKService<TaskBackNew> {
    @Autowired
    @Lazy(true)
    private SysTableService tableService;
    @Autowired
    private TaskBackNewDao taskBackNewDao;
    @Autowired
    @Lazy(true)
    private TaskUnitService taskUnitService;
    @Autowired
    @Lazy(true)
    private SysUserService sysUserService;
    @Autowired
    @Lazy(true)
    private DeptService deptService;
    @Autowired
    @Lazy(true)
    private SysLoginService sysLoginService;
    @Autowired
    @Lazy(true)
    private TaskLogNewService taskLogNewService;
    @Autowired
    @Lazy(true)
    private TaskUserService taskUserService;
    @Autowired
    @Lazy(true)
    private TaskMainService taskMainService;
    @Autowired
    @Lazy(true)
    private TaskSubService taskSubServer;
    @Autowired
    private RedisService redisService;
    

    @Autowired
    @Lazy
    private SpecialTaskService specialTaskService;

    @Override
    protected BaseDao<TaskBackNew, String> getDao() {
        return taskBackNewDao;
    }

    @Override
    protected void beforeSave(TaskBackNew obj) {
        obj.setCreateUser(obj.getCreateUser());
    }

    public void save(TaskBackNew taskBackNew, String userId) {
        Date date = new Date();
        String date1 = HolidayUtils.sdf1.format(date);
        String time = HolidayUtils.sdf2.format(date);
        taskBackNew.setSysId(tableService.updateMaxSysId("t_task_back_new", null));
        taskBackNew.setCreateUser(userId);
        taskBackNew.setUpdUser(userId);
        taskBackNew.setCreateDate(date1);
        taskBackNew.setCreateTime(time);
        taskBackNew.setUpdDate(date1);
        taskBackNew.setUpdTime(time);
        getDao().save(taskBackNew);
    }

    public void saveTaskBackNew(TaskBackNew taskBackNew) {
        taskBackNew.setSysId(tableService.updateMaxSysId("t_task_back_new", null));
        taskBackNewDao.save(taskBackNew);

    }

    public List<TaskBackNew> getByTaskUnitId(String taskUnitId) {
        return taskBackNewDao.getByTaskUnitId(taskUnitId);
    }

    /**
     * 承办单位反馈
     *
     * @param filter
     * @return
     */
    public String saveBack(TaskBackNewFilter filter) {
        Date date1 = new Date();
        String date = HolidayUtils.sdf1.format(date1);
        String time = HolidayUtils.sdf2.format(date1);
        String userId = "";
        if (StringUtils.isNotBlank(filter.getCreateId())) {
            userId = filter.getCreateId();
        } else {
            userId = SysUserUtils.getUserId();
        }
        SysUser user = sysUserService.getById(userId);
        if (ObjectUtils.isBlank(user)) {
            return "登录超时，请刷新页面重新登录";
        }

        SysLogin sysLogin = sysLoginService.getById(userId);
        SysDept dept = deptService.getById(sysLogin.getDeptId());
        if (StringUtils.isNotBlank(filter.getRecipientId())
                || StringUtils.isNotBlank(filter.getContent())
                || StringUtils.isNotBlank(filter.getStatus())) {
            TaskUnit taskUnit = taskUnitService.getById(filter.getTaskUnitId());
            TaskMain taskMain = taskMainService.getById(taskUnit.getTaskMainId());
            TaskBackNew taskBackNew = new TaskBackNew();
            BeanUtils.copyProperties(filter, taskBackNew);
            taskBackNew.setTaskSubId(taskUnit.getTaskSubId());
            taskBackNew.setDeptName(dept.getDeptName());
            taskBackNew.setTaskMainId(taskMain.getSysId());//主任务的id
            //如果用户是单位领导
            //  /任务状态(0.待签收,1待反馈,2已反馈,3退回,4完成 。5已签收)
            if ("02".equals(sysLogin.getRoleId())) {
                //设置该任务为已反馈
                if (!"4".equals(taskUnit.getType())) {
                    if("3".equals(taskUnit.getType())||"7".equals(taskUnit.getType()))
                    {
                        taskUnit.setType("8");//退单已反馈
                    }else {

                        taskUnit.setType("2");
                    }
                }
                //单位任务转为已办
                taskUnit.setState("1");
                //如果是牵头单位反馈
                if ("1".equals(taskUnit.getUnitType())) {
                    taskBackNew.setDeptType("1");//牵头单位反馈标志，用于 取出反馈的时候，区别牵头和协办
                    //主任务状态改为已反馈
                    //判断该任务是不是单位重大事项
                    if (StringUtils.isNotBlank(taskMain.getOverdueDept())) {
                        //是
                        //将单位重大事项的状态改为待办

                        taskBackNew.setRecipientId(taskMain.getOverdueDept());//设置为督办单位；
                        taskBackNew.setTimeRound(taskUnit.getTimeRound());
                        taskMain.setOverdueDeptState("0");
                    } else {
                        taskMain.setStatus("3");
                    }
                    taskMainService.update(taskMain);
                }
                taskUnitService.update(taskUnit);
                if ("2".equals(taskUnit.getUnitType())) {//如果是协办单位
                    taskBackNew.setDeptType("2");//协办单位反馈标志
                    //协办单位的领导反馈
//                    taskBackNew.setReceiveDept(filter.getRecipientId());
                    taskBackNew.setRecipientId(null);
                    //接收单位name
//                    SysDept dept1 = deptService.getById(taskBackNew.getReceiveDept());
//                    taskBackNew.setReceiveDeptName(dept1.getDeptName());
                    taskBackNew.setDeptName(dept.getDeptName());
                    //查询当前子任务的牵头单位
                    TaskUnitFilter unitFilter = new TaskUnitFilter();
                    unitFilter.setUnitType("1");
                    unitFilter.setTaskSubId(taskUnit.getTaskSubId());
                    List<TaskUnit> taskUnits = taskUnitService.queryFilter(unitFilter);//拿到所有牵头单位
                    for (TaskUnit unit : taskUnits) {
                        //设置牵头单位有新的反馈
                        unit.setInnerBack("1");
                        //牵头单位就有待办任务
                        unit.setState("0");
                        taskUnitService.update(unit);
                    }
                } else {
                    SysUser recipient = sysUserService.getById(filter.getRecipientId());
                    taskBackNew.setRecipientName(recipient.getName());
                }
            } else {
                //如果不是，更新人员任务
                SysUser recipient = sysUserService.getById(filter.getRecipientId());
                //反馈对象人员登录信息
                SysLogin recipientLogin = sysLoginService.getById(filter.getRecipientId());
                //如果反馈对象是单位
                if ("02".equals(recipientLogin.getRoleId())) {
                    if (!"4".equals(taskUnit.getType())) {
                        taskUnit.setInnerBack("1");
                        //单位任务转为待办
                        taskUnit.setState("0");
                    }
                    //设置当前单位任务有新的反馈
                    taskUnitService.update(taskUnit);
                } else {
                    //反馈对象不是单位，单位已办状态不变，查询反馈给那个人员任务了
                    List<TaskUser> unitUser = taskUserService.getByTaskUnitIdAndUnitUserId(taskUnit.getSysId(), recipient.getSysId());
                    for (TaskUser taskUser : unitUser) {
                        //反馈对象的人员任务就转为待办
                        if (!"4".equals(taskUser.getStatus())) {
                            taskUser.setState("0");
                        }
                        taskUserService.update(taskUser);
                    }

                }
                //当前反馈人员的任务转为已办
                List<TaskUser> taskUsers = taskUserService.getByTaskUnitIdAndUnitUserId(taskUnit.getSysId(), user.getSysId());
                for (TaskUser taskUser : taskUsers) {
                    if (!"4".equals(taskUser.getStatus())) {
                        taskUser.setStatus("2");
                        //当前人员的任务转入已办
                    }
                    taskUser.setState("1");
                    taskUserService.update(taskUser);
                }
                taskBackNew.setRecipientName(recipient.getName());
            }
            taskBackNew.setSenderName(user.getName());
            taskBackNew.setStatus("3");

            this.save(taskBackNew, userId);

            TaskLogNew taskLogNew = new TaskLogNew();
            String content = "";
            taskLogNew.setTaskUnitId(filter.getTaskUnitId());
            if ("2".equals(taskUnit.getUnitType()) && "02".equals(sysLogin.getRoleId())) {
                taskLogNew.setTaskSubId(taskUnit.getTaskSubId());
                taskLogNew.setDept(taskBackNew.getReceiveDept());
//                SysDept receiveDept = deptService.getById(taskBackNew.getReceiveDept());
                content = user.getName() + "反馈给牵头单位";
                /*+ receiveDept.getDeptName()*/
            } else if ("1".equals(taskUnit.getUnitType()) && "02".equals(sysLogin.getRoleId())) {
                content = user.getName() + "反馈给督查单位";
            } else {
                taskLogNew.setUserId(taskBackNew.getRecipientId());
                SysUser receiveUser = sysUserService.getById(taskBackNew.getRecipientId());
                content = user.getName() + "反馈给" + receiveUser.getName();
            }
            taskLogNew.setContent(content);
            taskLogNewService.save(taskLogNew, userId);
            int t0 = taskUnitService.getCountByType(taskMain.getSysId(), "0");
            int t1 = taskUnitService.getCountByType(taskMain.getSysId(), "1");
            int t2 = taskUnitService.getCountByType(taskMain.getSysId(), "2");
            int t3 = taskUnitService.getCountByType(taskMain.getSysId(), "3");
            int t4 = taskUnitService.getCountByType(taskMain.getSysId(), "4");
            int t5 = taskUnitService.getCountByType(taskMain.getSysId(), "5");
            int o = taskUnitService.getCountByStatus(taskMain.getSysId(), "10");
            taskMain.setTypeExplain("待签收: " + t0 + "；已签收：" + t5 + " ；待反馈: " + t1 + "; 已反馈: " + t2 + "; 重办: " + t3 + "; 完成: " + t4);

            taskMain.setOverdueExplain("逾期单位任务: " + o);
            taskMainService.update(taskMain);
            return "200";
        } else {
            return "500";
        }

    }


    /**
     * 保存攻坚任务督查组的反馈
     * 以当前反馈轮次为最新轮次，不去管子任务的轮次
     *
     */
    @Transactional
    public String saveGongJianTaskBack(TaskBackNewFilter filter)
    {
        //拿到特殊任务表中 最新任务
        String taskMainId  = filter.getTaskMainId();
        TaskMain taskMain = taskMainService.getById(taskMainId);
        TaskUnit taskUnit = taskUnitService.getById(filter.getTaskUnitId());

        String deptId = filter.getDeptId();
        String userId = filter.getLoginerId();
        SpecialTaskFilter spfilter = new SpecialTaskFilter();
        spfilter.setTaskMainId(taskMainId);
        spfilter.setInspectionId(deptId);

        List<SpecialTask> specialTasks = specialTaskService.queryFilter(spfilter);
        SpecialTask specialTask = specialTasks.get(0);//取出最新的一个轮次，作为最新的反馈轮次

        // data.setDeptName(sysDept.getDeptName());

        TaskBackNew taskback  = new TaskBackNew();
        taskback.setDeptType("4");//督查组； //1 牵头， 2 协办 ，3 督办单位；4 攻坚任务 5 内部
        taskback.setContent(filter.getContent());//反馈内容
        taskback.setPlan(filter.getPlan());
        taskback.setProblem(filter.getProblem());
        taskback.setTaskUnitId(specialTask.getSysId());
        taskback.setTaskSubId(ObjectUtils.isBlank(taskUnit)?"":taskUnit.getTaskSubId());

        try {

            String strDept = redisService.get("dept:" + deptId);
            SysDept sysDept = JSONObject.parseObject(strDept, SysDept.class);
            SysUser loginUser = sysUserService.getById(userId);
            taskback.setSenderName(loginUser.getName());
            taskback.setDeptName(sysDept.getDeptName());//反馈单位名称
        }catch (Exception e){
            return 500+"";
        }

        taskback.setStatus("1");
        taskback.setFile(filter.getFile());
        taskback.setFileName(filter.getFileName());
        taskback.setTimeRound(specialTask.getTimeRound());//轮次
        taskback.setTaskMainId(taskMainId);

        this.save(taskback,userId);

        //0.待签收,1待反馈,2已反馈,3退回,4完成 。5已签收)
        specialTask.setType("2");
        specialTaskService.update(specialTask);

        //流程记录
        TaskLogNew log = new TaskLogNew();
        log.setContent("督办单位:"+" 已反馈给作战室");
        taskLogNewService.save(log,userId);
        //更新主表
        int t0 = taskUnitService.getCountByType(taskMainId, "0");
        int t1 = taskUnitService.getCountByType(taskMainId, "1");
        int t2 = taskUnitService.getCountByType(taskMainId, "2");
        int t3 = taskUnitService.getCountByType(taskMainId, "3");
        int t4 = taskUnitService.getCountByType(taskMainId, "4");
        int t5 = taskUnitService.getCountByType(taskMainId, "5");
        int o= taskUnitService.getCountByStatus(taskMainId, "10");
        taskMain.setTypeExplain("待签收: " + t0 + "；已签收：" + t5 + " ；待反馈: " + t1 + "; 已反馈: " + t2 + "; 重办: " + t3 + "; 完成: " + t4);

        taskMain.setOverdueExplain("逾期单位任务: " + o);
       // taskMain.setMainResult(filter.getContent());
      //  taskMain.setMainResultFile(filter.getFile());
       // taskMain.setMainResultFileName(filter.getFileName());
        taskMainService.update(taskMain);

        return  "200";



    }

    /**
     * 保存督办单位的反馈信息
     * @param filter
     * @return
     */
    @Transactional
    public  String saveDuBanBackInfo(TaskBackNewFilter filter){

        String userId = SysUserUtils.getUserId();
        SysUser user = sysUserService.getById(userId);

        TaskMain taskMain = taskMainService.getById(filter.getTaskMainId());//拿到主任务
        if(ObjectUtils.isBlank(taskMain))
        {
            return "500";
        }

        TaskBackNew taskback  = new TaskBackNew();
        taskback.setDeptType("3");
        taskback.setContent(filter.getContent());//反馈内容
        taskback.setDeptName(filter.getDeptName());//反馈单位名称
        taskback.setStatus("3");
        taskback.setFile(filter.getFile());
        taskback.setFileName(filter.getFileName());
        taskback.setTaskMainId(taskMain.getSysId());
        SpecialTaskFilter spFilter = new SpecialTaskFilter();
        spFilter.setTaskMainId(taskMain.getSysId());
        List<SpecialTask> specialTasks = specialTaskService.queryFilter(spFilter);
        taskback.setTaskUnitId(specialTasks.get(0).getSysId());//特殊任务表的id
        this.save(taskback,userId);
        //流程记录
        TaskLogNew log = new TaskLogNew();
        log.setContent("督办单位:"+user.getName()+" 已反馈给督查组");
        taskLogNewService.save(log,userId);
        //更新主表
        int t0 = taskUnitService.getCountByType(taskMain.getSysId(), "0");
        int t1 = taskUnitService.getCountByType(taskMain.getSysId(), "1");
        int t2 = taskUnitService.getCountByType(taskMain.getSysId(), "2");
        int t3 = taskUnitService.getCountByType(taskMain.getSysId(), "3");
        int t4 = taskUnitService.getCountByType(taskMain.getSysId(), "4");
        int t5 = taskUnitService.getCountByType(taskMain.getSysId(), "5");
        int o = taskUnitService.getCountByStatus(taskMain.getSysId(), "10");
        taskMain.setTypeExplain("待签收: " + t0 + "；已签收：" + t5 + " ；待反馈: " + t1 + "; 已反馈: " + t2 + "; 重办: " + t3 + "; 完成: " + t4);

        taskMain.setOverdueExplain("逾期单位任务: " + o);
        taskMain.setMainResult(filter.getContent());
        taskMain.setMainResultFile(filter.getFile());
        taskMain.setMainResultFileName(filter.getFileName());
        taskMainService.update(taskMain);
        return "200";
    }


    public List<TaskBackNew> getByTaskUnitIdAndSenderId(String taskUnitId, String userId) {
        return taskBackNewDao.getByTaskUnitIdAndSenderId(taskUnitId, userId);
    }

    public List<TaskBackNew> getByTaskUnitIdAndRecipientId(String taskUnitId, String userId) {
        return taskBackNewDao.getByTaskUnitIdAndRecipientId(taskUnitId, userId);

    }

    public List<TaskBackNew> getByTaskUnitIdAndReceiveDeptAndSubId(String taskUnitId, String receiveDept, String subId) {
        return taskBackNewDao.getByTaskUnitIdAndReceiveDeptAndSubId(taskUnitId, receiveDept, subId);

    }

    /**
     * app单位用户重办
     *
     * @param filter
     * @return
     */
    public String updateUnitStatus(TaskBackNewFilter filter) {
        String taskBackId = filter.getSysId();
        String userId = filter.getUserId();
        SysUser user = sysUserService.getById(userId);
        SysLogin login = sysLoginService.getById(userId);
        SysDept dept = deptService.getById(login.getDeptId());
        TaskBackNew taskBackNew = taskBackNewDao.getById(taskBackId);
        String createUser = taskBackNew.getCreateUser();
        TaskUnit taskUnit = taskUnitService.getById(taskBackNew.getTaskUnitId());
        TaskMain taskMain = taskMainService.getById(taskUnit.getTaskMainId());

        taskBackNew.setStatus(filter.getStatus());
        taskBackNewDao.update(taskBackNew);
        TaskLogNew taskLogNew = new TaskLogNew();
        SysUser byId = sysUserService.getById(taskBackNew.getCreateUser());
        taskLogNew.setUserId(taskBackNew.getCreateUser());
        taskLogNew.setTaskUnitId(taskBackNew.getTaskUnitId());
        String content = "";
        String status = "";
        //单位没有判断是否合格，只有重办才会发请求
        if ("2".equals(filter.getStatus())) {
            status = "重办";
            //如果是督查人员办理
            if ("/01/".equals(dept.getDeptRelation())) {
                taskUnit.setType("3");
                if (StringUtils.isBlank(taskUnit.getBackNum())){
                    taskUnit.setBackNum("1");
                }else{
                    int i = Integer.parseInt(taskUnit.getBackNum());
                    i++;
                    taskUnit.setBackNum(i+"");
                }
                taskUnitService.update(taskUnit);
                //单位任务处理后将主任务状态改为正常
                taskMain.setStatus("2");
                taskMainService.update(taskMain);
            } else {
                //判断这条信息是本单位还是协办单位的反馈
                String deptId = login.getDeptId();
                if (deptId.equals(dept.getSysId())) {
//                    本单位
                    //如果不是，根据反馈信息查询是哪个单位人员反馈的
                    List<TaskUser> taskUsers = taskUserService.getByTaskUnitIdAndUnitUserId(taskUnit.getSysId(), createUser);
                    for (TaskUser taskUser : taskUsers) {
                        taskUser.setStatus("3");
                        if (StringUtils.isBlank(taskUser.getBackNum())){
                            taskUser.setBackNum("1");
                        }else{
                            int i = Integer.parseInt(taskUser.getBackNum());
                            i++;
                            taskUser.setBackNum(i+"");
                        }
                        //退回从半转入待办
                        taskUser.setState("0");
                        taskUserService.update(taskUser);
                    }

                } else {
                    //不是本单位，为协办单位，查询当前协办单位的单位任务
                    taskUnit.setType("3");
                    //协办单位任务转入待办
                    taskUnit.setState("0");
                    if (StringUtils.isBlank(taskUnit.getBackNum())){
                        taskUnit.setBackNum("1");
                    }else{
                        int i = Integer.parseInt(taskUnit.getBackNum());
                        i++;
                        taskUnit.setBackNum(i+"");
                    }
                    taskUnitService.update(taskUnit);
                }
            }
        }
        content = user.getName() + "办理" + byId.getName() + "的反馈信息：" + status;
        taskLogNew.setContent(content);
        taskLogNewService.save(taskLogNew, userId);
        //反馈状态判断后将当前单位反馈的是否有内部反馈状态改为没有
        taskUnit.setInnerBack("0");
        taskUnitService.update(taskUnit);

        int t0 = taskUnitService.getCountByType(taskMain.getSysId(), "0");
        int t1 = taskUnitService.getCountByType(taskMain.getSysId(), "1");
        int t2 = taskUnitService.getCountByType(taskMain.getSysId(), "2");
        int t3 = taskUnitService.getCountByType(taskMain.getSysId(), "3");
        int t4 = taskUnitService.getCountByType(taskMain.getSysId(), "4");
        int t5 = taskUnitService.getCountByType(taskMain.getSysId(), "5");
        int o = taskUnitService.getCountByStatus(taskMain.getSysId(), "10");
        taskMain.setTypeExplain("待签收: " + t0 + "；已签收：" + t5 + " ；待反馈: " + t1 + "; 已反馈: " + t2 + "; 重办: " + t3 + "; 完成: " + t4);
        taskMain.setOverdueExplain("逾期单位任务: " + o);
        taskMain.setStatus("3");//新反馈
        taskMainService.update(taskMain);
        return "200";
    }

    /**
     * 更新单位反馈状态
     *
     * @param taskBackId
     * @param status
     * @return
     */
    public String updateStatus(String taskBackId, String status) {

        SysUser user = sysUserService.getById(SysUserUtils.getUserId());
        SysDept dept = deptService.getById(SysUserUtils.getDeptId());
        TaskBackNew taskBackNew = taskBackNewDao.getById(taskBackId);
        String createUser = taskBackNew.getCreateUser();
        TaskUnit taskUnit = taskUnitService.getById(taskBackNew.getTaskUnitId());
        TaskMain taskMain = taskMainService.getById(taskUnit.getTaskMainId());

        taskBackNew.setStatus(status);
        taskBackNewDao.update(taskBackNew);
        TaskLogNew taskLogNew = new TaskLogNew();
        SysUser byId = sysUserService.getById(taskBackNew.getCreateUser());
        taskLogNew.setUserId(taskBackNew.getCreateUser());
        taskLogNew.setTaskUnitId(taskBackNew.getTaskUnitId());
        String content = "";
        //单位没有判断是否合格，只有重办才会发请求
        if ("2".equals(status)) {/*
            status = "合格";
            //如果是督查人员办理
            if ("/01/".equals(dept.getDeptRelation())){
                taskUnit.setType("4");
                taskUnitService.update(taskUnit);
                //单位任务处理后将主任务状态改为正常
                taskMain.setStatus("2");
                taskMainService.update(taskMain);
            }else {
                //判断这条信息是本单位还是协办单位的反馈
                SysLogin login = sysLoginService.getById(createUser);
                String deptId = login.getDeptId();
                if (deptId.equals(dept.getSysId())){
//                    本单位
                    //如果不是，根据反馈信息查询是哪个单位人员反馈的
                    List<TaskUser> taskUsers = taskUserService.getByTaskUnitIdAndUnitUserId(taskUnit.getSysId(), createUser);
                    taskUsers.get(0).setStatus("4");
                    taskUserService.update(taskUsers.get(0));
                }else{
                    //不是本单位，为协办单位，查询当前协办单位的单位任务
                    taskUnit.setType("4");
                    taskUnitService.update(taskUnit);
                }
            }
        } else {*/
            status = "重办";
            //如果是督查人员办理
            if ("/01/".equals(dept.getDeptRelation())) {
                taskUnit.setType("3");
                taskUnitService.update(taskUnit);
                //单位任务处理后将主任务状态改为正常
                taskMain.setStatus("2");
                taskMainService.update(taskMain);
            } else {
                //判断这条信息是本单位还是协办单位的反馈
                SysLogin login = sysLoginService.getById(createUser);
                String deptId = login.getDeptId();
                if (deptId.equals(dept.getSysId())) {
//                    本单位
                    //如果不是，根据反馈信息查询是哪个单位人员反馈的
                    List<TaskUser> taskUsers = taskUserService.getByTaskUnitIdAndUnitUserId(taskUnit.getSysId(), createUser);
                    for (TaskUser taskUser : taskUsers) {
                        taskUser.setStatus("3");
                        //退回从半转入待办
                        taskUser.setState("0");
                        taskUserService.update(taskUser);
                    }

                } else {
                    //不是本单位，为协办单位，查询当前协办单位的单位任务
                    taskUnit.setType("3");
                    //协办单位任务转入待办
                    taskUnit.setState("0");
                    taskUnitService.update(taskUnit);
                }
            }
        }
        content = user.getName() + "办理" + byId.getName() + "的反馈信息：" + status;
        taskLogNew.setContent(content);
        taskLogNewService.save(taskLogNew);
        //反馈状态判断后将当前单位反馈的是否有内部反馈状态改为没有
        taskUnit.setInnerBack("0");
        taskUnitService.update(taskUnit);

        int t0 = taskUnitService.getCountByType(taskMain.getSysId(), "0");
        int t1 = taskUnitService.getCountByType(taskMain.getSysId(), "1");
        int t2 = taskUnitService.getCountByType(taskMain.getSysId(), "2");
        int t3 = taskUnitService.getCountByType(taskMain.getSysId(), "3");
        int t4 = taskUnitService.getCountByType(taskMain.getSysId(), "4");
        int t5 = taskUnitService.getCountByType(taskMain.getSysId(), "5");
        int o = taskUnitService.getCountByStatus(taskMain.getSysId(), "10");
        taskMain.setTypeExplain("待签收: " + t0 + "；已签收：" + t5 + " ；待反馈: " + t1 + "; 已反馈: " + t2 + "; 重办: " + t3 + "; 完成: " + t4);
        taskMain.setOverdueExplain("逾期单位任务: " + o);
        taskMainService.update(taskMain);
        return "200";
    }

    public List<TaskBackNew> getByRecipientIdAndSubId(String recipientId, String taskSubId) {
        return taskBackNewDao.getByRecipientIdAndSubId(recipientId, taskSubId);
    }

    public void deleteByUnitIds(List<String> taskUnitIds) {
        taskBackNewDao.deleteByUnitIds(taskUnitIds);
    }


    public List<TaskBackNew> queryBeforeDate(String pastDate) {
        return taskBackNewDao.queryBeforeDate(pastDate);
    }


    //多维度查询入口
    public List<TaskBackNew> queryAll(TaskBackNewFilter filter)
    {
        return taskBackNewDao.queryAll(filter);
    }


    /**
     * 督办单位反馈记录，
     * @param filter
     * @return
     */
    public List<TaskBackNew> getListBacks(TaskBackNewFilter filter)
    {
        // private String deptType;//1 牵头， 2 协办 ，3 督办单位； 4 攻坚任务  5 内部

        /*
        攻坚任务
         */
        if("4".equals(filter.getDeptType()))
        {
            String gjId = filter.getGjId();
            TaskMainFilter gjfilter = new TaskMainFilter();
            gjfilter.setGjId(gjId);
            List<TaskMain> taskMains = taskMainService.queryFilter(gjfilter);
            List<TaskBackNew> list = new ArrayList<>();
            for (TaskMain taskMain : taskMains) {
                filter.setTaskMainId(taskMain.getSysId());
                List<TaskBackNew> taskBackNews = taskBackNewDao.queryAll(filter);
                if (ObjectUtils.isNotBlank(taskBackNews)) {
                    TaskBackNew taskBackNew = taskBackNews.get(0);//最新的一次反馈
                    String createUser = taskBackNew.getCreateUser();

                    SysUser user = sysUserService.getById(createUser);
                    taskBackNew.setUserName(user.getName());
                    List<AttachmentsDTO> filedot = new ArrayList<>();
                    String file = taskBackNew.getFile();
                    if (StringUtils.isNotBlank(file)) {

                        String[] fileSplit = file.split(",");
                        String[] splitName = taskBackNew.getFileName().split(",");
                        for (int i = 0; i < fileSplit.length; i++) {
                            filedot.add(new AttachmentsDTO(fileSplit[i], splitName[i]));
                        }
                        taskBackNew.setFileDto(filedot);
                    }
                    list.add(taskBackNew);

                }
            }
            return  list;
        }else {
            filter.setDeptType(3+"");
            List<TaskBackNew> taskBackNews = taskBackNewDao.queryAll(filter);

            for (TaskBackNew item : taskBackNews) {

                String createUser = item.getCreateUser();

                SysUser user = sysUserService.getById(createUser);
                item.setUserName(user.getName());
                List<AttachmentsDTO> filedot = new ArrayList<>();
                String file = item.getFile();
                if(StringUtils.isNotBlank(file))
                {

                    String[] fileSplit = file.split(",");
                    String[] splitName = item.getFileName().split(",");
                    for (int i = 0; i < fileSplit.length; i++) {
                        filedot.add( new AttachmentsDTO(fileSplit[i],splitName[i]));
                    }
                    item.setFileDto(filedot);
                }


            }
            return taskBackNews;
        }




    }

    /**
     * 牵头单位的反馈记录
     * @param filter
     * @return
     */
    public List<HashMap<String, Object>> getQianTouBacks(TaskBackNewFilter filter){
    // private String deptType;//1 牵头， 2 协办 ，3 督办单位；
        filter.setDeptType(1+"");
        List<TaskBackNew> taskBackNews = taskBackNewDao.queryAll(filter);
        // 按照子任务id分组；因为 同个子任务有多次反馈记录；
        Map<String, List<TaskBackNew>> subTaskMap = taskBackNews.stream().collect(Collectors.groupingBy(TaskBackNew::getTaskSubId));
        List<HashMap<String,Object>> data = new ArrayList<>();
        for(Map.Entry<String ,List<TaskBackNew>> entry:subTaskMap.entrySet())
        {
            String subTaskId = entry.getKey();
            //获取子任务
            TaskSub taskSub = taskSubServer.getById(subTaskId);

            List<TaskBackNew> backLists = entry.getValue();
            for (TaskBackNew backList : backLists) {
                List<AttachmentsDTO> filedot = new ArrayList<>();
                String file = backList.getFile();
                if(StringUtils.isNotBlank(file))
                {

                    String[] fileSplit = file.split(",");
                    String[] splitName = backList.getFileName().split(",");
                    for (int i = 0; i < fileSplit.length; i++) {
                        filedot.add( new AttachmentsDTO(fileSplit[i],splitName[i]));
                    }
                    backList.setFileDto(filedot);
                }
            }
            HashMap<String,Object>  subMap = new HashMap<>();
            subMap.put("taskSubName",taskSub.getSubName());
            subMap.put("taskSubList",backLists);
            data.add(subMap);

        }

        return data;
    }





}
